package com.xiaohong.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.goods.pojo.SpecGroup;
import com.xiaohong.goods.pojo.SpecParam;
import com.xiaohong.goods.service.SpecGroupService;
import com.xiaohong.goods.service.SpecParamService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "SpecGroup接口文档")
@RestController
@RequestMapping("/specGroup")
@CrossOrigin
public class SpecGroupController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /***
     * 分页条件搜索实现SpecGroup
     * @param specGroup
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询SpecGroup",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "SpecGroup对象",required = true) SpecGroup specGroup, @PathVariable int page, @PathVariable  int size){
        //调用SpecGroupService实现分页条件查询SpecGroup
        Page pageInfo = specGroupService.findPage(specGroup, page, size);
        // 获取查询到的数据
        List<SpecGroup> specGroupList = pageInfo.getRecords();
        // 添加分组下参数信息
        for (SpecGroup group : specGroupList) {
            group.setGroupParam(specParamService.findAllByGroupName(group.getName()));
        }
        // 重新填会pageInfo
        pageInfo.setRecords(specGroupList);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现SpecGroup
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询SpecGroup",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SpecGroupService实现分页查询SpecGroup
        Page pageInfo = specGroupService.findPage(page, size);
        // 获取查询到的数据
        List<SpecGroup> specGroupList = pageInfo.getRecords();
        // 添加分组下参数信息
        for (SpecGroup group : specGroupList) {
            group.setGroupParam(specParamService.findAllByGroupName(group.getName()));
        }
        // 重新填会pageInfo
        pageInfo.setRecords(specGroupList);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索SpecGroup数据
     * @param specGroup
     * @return
     */
    @Operation(summary = "多条件查询SpecGroup",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<SpecGroup>> findList(@RequestBody(required = false) @Parameter(name = "SpecGroup对象",required = true) SpecGroup specGroup){
        //调用SpecGroupService实现条件查询SpecGroup
        List<SpecGroup> list = specGroupService.findList(specGroup);
        return new Result<List<SpecGroup>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除specGroup数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除SpecGroup",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用SpecGroupService实现根据ID删除
        specGroupService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改SpecGroup数据
     * @param specGroup
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改SpecGroup",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "SpecGroup对象",required = true) SpecGroup specGroup,@PathVariable Integer id){
        // 补充id
        specGroup.setId(id);
        //调用SpecGroupService实现修改SpecGroup
        specGroupService.update(specGroup);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增SpecGroup数据
     * @param specGroup
     * @return
     */
    @Operation(summary = "添加SpecGroup",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "SpecGroup对象",required = true) SpecGroup specGroup){
        try {
            // 判断是否时重复添加
            if (specGroupService.findList(specGroup).size() != 0){
                return new Result(true,StatusCode.PARAMETERERROR,"重复添加！");
            }
            //调用SpecGroupService实现添加SpecGroup
            specGroupService.add(specGroup);
            return new Result(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.PARAMETERERROR,"添加失败");
        }
    }

    /***
     * 根据ID查询SpecGroup数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询SpecGroup",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<SpecGroup> findById(@PathVariable Integer id){
        //调用SpecGroupService实现根据ID查询SpecGroup
        SpecGroup specGroup = specGroupService.findById(id);
        // 添加分组下参数信息
        specGroup.setGroupParam(specParamService.findAllByGroupName(specGroup.getName()));
        return new Result<SpecGroup>(true,StatusCode.OK,"查询成功",specGroup);
    }

    /***
     * 根据分类ID查询所有SpecGroup数据
     * @param categoryId
     * @return
     */
    @Operation(summary = "根据分类ID查询所有SpecGroup数据",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "categoryId", description = "分类ID", required = true)
    @GetMapping("/category/{categoryId}")
    public Result<Map<String, Object>> findByCategoryId(@PathVariable Integer categoryId){
        //调用SpecGroupService实现根据ID查询SpecGroup
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCategoryId(categoryId);
        List<SpecGroup> specGroupList = specGroupService.findList(specGroup);
        // 添加分组下参数信息
        for (SpecGroup group : specGroupList) {
            group.setGroupParam(specParamService.findAllByGroupName(group.getName()));
        }
        return new Result<Map<String, Object>>(true,StatusCode.OK,"查询成功",specGroupList);
    }

    /***
     * 根据分类ID查询SpecGroup通用数据
     * @param categoryId
     * @return
     */
    @Operation(summary = "根据分类ID查询SpecGroup通用数据",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "categoryId", description = "分类ID", required = true)
    @GetMapping("/category/general/{categoryId}")
    public Result<Map<String, Object>> findGeneralByCategoryId(@PathVariable Integer categoryId){
        //调用SpecGroupService实现根据ID查询SpecGroup
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCategoryId(categoryId);
        List<SpecGroup> specGroupList = specGroupService.findList(specGroup);
        // 添加分组下参数信息
        for (SpecGroup group : specGroupList) {
            group.setGroupParam(specParamService.findGeneralByGroupName(group.getName()));
        }
        return new Result<Map<String, Object>>(true,StatusCode.OK,"查询成功",specGroupList);
    }

    /***
     * 根据分类ID查询SpecGroup非通用数据
     * @param categoryId
     * @return
     */
    @Operation(summary = "根据分类ID查询SpecGroup非通用数据",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "categoryId", description = "分类ID", required = true)
    @GetMapping("/category/ungeneral/{categoryId}")
    public Result<Map<String, Object>>  findUnGeneralByCategoryId(@PathVariable Integer categoryId){
        //调用SpecGroupService实现根据ID查询SpecGroup
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCategoryId(categoryId);
        List<SpecGroup> specGroupList = specGroupService.findList(specGroup);
        // 添加分组下参数信息
        for (SpecGroup group : specGroupList) {
            group.setGroupParam(specParamService.findUnGeneralByGroupName(group.getName()));
        }
        return new Result<Map<String, Object>>(true,StatusCode.OK,"查询成功",specGroupList);
    }


    /***
     * 查询SpecGroup全部数据
     * @return
     */
    @Operation(summary = "查询所有SpecGroup",description = "所需权限：")
    @GetMapping
    public Result<List<SpecGroup>> findAll(){
        //调用SpecGroupService实现查询所有SpecGroup
        List<SpecGroup> list = specGroupService.findAll();
        return new Result<List<SpecGroup>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
